package com.jiyun.serviceImpl;

import com.jiyun.entity.Result;
import com.jiyun.mapper.DutyMapper;
import com.jiyun.mapper.EmployeeMapper;
import com.jiyun.mapper.SectorMapper;
import com.jiyun.pojo.Duty;
import com.jiyun.pojo.Employee;
import com.jiyun.pojo.Sector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class EmployeeServiceImpl {

    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    DutyMapper dutyMapper;
    @Autowired
    SectorMapper sectorMapper;

    public List<Employee> show() {
        List<Employee> list = employeeMapper.selectAll();
        for (Employee employee : list) {
            employee.setSectorList(sectorMapper.selectByPrimaryKey(employee.getSid()));
            employee.setDutyList(dutyMapper.selectByPrimaryKey(employee.getDid()));
        }
        return list;
    }


    public void del(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public List<Duty> findzhi() {
        List<Duty> duties = dutyMapper.selectAll();
        return duties;
    }

    public List<Sector> findbu() {
        List<Sector> sectors = sectorMapper.selectAll();
        return sectors;
    }

    public void add(Employee employee) {
        employeeMapper.insertSelective(employee);
    }

    public void update(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public List<Duty> findPagesec() {
        List<Duty> duties = dutyMapper.selectAll();
        for (Duty duty : duties) {
            duty.setSector(sectorMapper.selectByPrimaryKey(duty.getSid()));
        }
        return duties;
    }

    public void delsec(Integer id) {
        Employee employee1 = new Employee();
        employee1.setDid(id);
        List<Employee> employee = employeeMapper.select(employee1);
        Duty duty = dutyMapper.selectByPrimaryKey(id);
        Sector sector = sectorMapper.selectByPrimaryKey(duty.getSid());
        if (employee==null || employee.size()==0){
            dutyMapper.deleteByPrimaryKey(id);
            sectorMapper.deleteByPrimaryKey(sector.getId());
        }
    }

    public void addsec(Sector sector) {
        sectorMapper.insertSelective(sector);
    }

    public void updatesec(Duty duty) {
        dutyMapper.updateByPrimaryKeySelective(duty);
    }

    public void zhiwuadd(Duty duty) {
        dutyMapper.insertSelective(duty);
    }
}
